package cms.simmytech.web.tbuser;

import cms.simmytech.model.TbUser;
import cms.simmytech.response.BaseResponse;
import cms.simmytech.response.UserDetailResponse;
import cms.simmytech.service.TbUserService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/tbUser")
public class TbUserController extends BaseController {
//    @Autowired
//    private TbUserMapper tbUserMapper;
    @Autowired
    private TbUserService service;
    @RequestMapping(value = "/TbUserList")
    public String majorList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model
    ,String name ,Byte  userGender,Byte userState) {
        PageInfo<TbUser> pageInfo = service.getTbUserList(pageNum, pageSize,name,userGender,userState);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("userGender", userGender);
        model.addAttribute("userState", userState);
        return super.toPage("tbUser/TbUserList", model);
    }

    //展示或填写TbUser信息
    @RequestMapping("/TbUserDetail")
    public String TbUserDetail(@RequestParam(value = "id") Integer id,Model model){//用来显示一个专业的详细信息
        UserDetailResponse response=service.getUserDetail(id);
        model.addAttribute("detail",response.getData());
        return super.toPage("tbUser/TbUserDetail", model);
    }
    //新增或者修改信息
    @PostMapping(value = "/TbUserSave")
    public String TbUserSave(TbUser tbuser,Model model) {
        service.saveTbUser(tbuser);
        return "redirect:TbUserList";

    }
    @RequestMapping("/edit")
    public String edit(@RequestParam(value = "id") Integer id,Model model){//用来显示一个专业的详细信息
        TbUser tbUser=null;
        tbUser=service.findTbUserById(id);
        model.addAttribute("tbUser",tbUser);
        return super.toPage("tbUser/edit", model);
    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public BaseResponse update(Integer uId, Byte state) {
        return service.updateUser(uId, state);
    }
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> delete(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        service.delete(id);
        map.put("res", true);
        return map;
    }

}
